package com.yuandengta.yomall.auth.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * JWT配置 {@link JwtProperties}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */

@Component
@ConfigurationProperties(prefix = "auth.jwt")
@Slf4j
@Data
public class JwtProperties {

    private String pubKeyPath;
    private String priKeyPath;
    private String secret;
    private String cookieName;
    private Integer expire;
    private String unick;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    /**
     * 该方法在构造方法执行之后执行
     */
    @PostConstruct
    public void init() {
//        try {
//            File pubFile = new File(pubKeyPath);
//            File priFile = new File(priKeyPath);
//            // 如果公钥或者私钥不存在，重新生成公钥和私钥
//            /*需要创建本地的yomall 和 res 文件夹*/
//            if (!pubFile.exists() || !priFile.exists()) {
//                RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
//            }
//            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
//            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
//            System.out.println("公钥：" + publicKey);
//            System.out.println("私钥：" + privateKey);
//        } catch (Exception e) {
//            log.error("生成公钥和私钥出错");
//            e.printStackTrace();
//        }
    }
}
